﻿/**
 * ...
 * @author Артём Григорьев
 */

package tab;

import flash.Lib;
import gui.hug.HUGSprite;
import gui.hug.HUGTabbedPanel;
import gui.hug.Tab;

import list.NewList;
import list.OpenTabEvent;

import loader.Loader;

import client.Channel;
import client.Item;

class CategoryTab extends Tab
{
	private var categoryId :	UInt;
	private var categoryName :	String;
	private var loader : 		Loader;
	private var newsList : 		NewList;
	
	public function new(categoryId	 : UInt, 
						categoryName : String, 
						panel 		 : HUGTabbedPanel = null, 
						uid 		 : Int) 
	{
		// initialization
		newsList = new NewList(4, 7, 600);
		Loader.createInstance();
		loader = Loader.getInstance();
		
		this.categoryId = categoryId;
		this.categoryName = categoryName;
		
		// creating layout
		var sprite : HUGSprite = new HUGSprite();
		sprite.addChild(newsList);
		
		// loading data from server
		loader.getItemsByCategory(onLoad, categoryId);
		
		// listen for opening new tabs
		var onOpenTabEvent = function(e : OpenTabEvent) {
			var channel : Channel = cast(e.getContent(), Channel);
			var tab = new ItemsTab(channel.getChanId(), channel.getName(), panel, uid);
			tab.setId(channel.getChanId());
			panel.addTab(tab); 
		};
		newsList.addEventListener(OpenTabEvent.OPEN_TAB, onOpenTabEvent);
		
		super(sprite, categoryName, 3, panel, true);
	}

	
	public function onLoad(data : Array<Item>) : Void
	{
		for (item in data)
		{
		newsList.addItem(item,1);}
	}	
	
}